﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using PS.Practices.Provisioner.Provisioner.Util;
using PS.Practices.SiteBuilder.Features.Schema;
using System.Xml.XPath;
using System.Xml;

namespace PS.Practices.Provisioner.Layouts.PS.Practices.Provisioner
{
    public partial class Debug : LayoutsPageBase
    {
        protected override void OnInit(EventArgs e)
        {
            GetScriptNames0();
            DropDown1.AutoPostBack = true;
        }

        /* Set drop down items using data binding */
        protected void GetScriptNames0()
        {
            string sUrl = SPContext.Current.Site.Url;

            using(SPSite siteObject = new SPSite(sUrl))
            {
                using(SPWeb webObject = siteObject.RootWeb)
                {
                    try
                    {
                        SPListCollection myListCollection = webObject.Lists;
                        SPList myList = myListCollection["Provisioner Scripts"];
                        SPListItem item = myList.Items[0];
                        DropDown1.DataSource = myList.Items;
                        DropDown1.DataValueField = "Title";
                        DropDown1.DataTextField = "Title";
                        DropDown1.DataBind();
                    }
                    catch(SPException)
                    {
                    }
                }
            }
        }

        protected void GetScriptNames()
        {
            string sURL = SPContext.Current.Site.Url;
           
            using (SPSite siteObject = new SPSite(sURL))
            {
                using (SPWeb webObject = siteObject.RootWeb)
                {
                    try
                    {
                        SPListCollection myListCollection = webObject.Lists;
                        SPList myList = myListCollection["Provisioner Scripts"];
                        SPListItemCollection items = myList.GetItems();
                        DropDown1.Items.Clear();
                        foreach(SPListItem item in items)
                        {
                            string str = item["Title"].ToString();
                            DropDown1.Items.Add(str);
                        }
                    }
                    catch(SPException)
                    {
                    }
                }
            }
        }

        // Display script contents in text box
        protected void DropDown1_SelectedIndexChanged(object sender, EventArgs eventArgs)
        {
            string selectedItem = DropDown1.SelectedValue.ToString();
            Console.WriteLine(DropDown1.SelectedIndex.ToString());

            string sURL = SPContext.Current.Site.Url;

            using (SPSite siteObject = new SPSite(sURL))
            {
                using (SPWeb webObject = siteObject.RootWeb)
                {
                    try
                    {
                        SPQuery query = new SPQuery() { RowLimit = Constants.SpQueryRowLimit };
                        query.Query = String.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", selectedItem);

                        SPListCollection myListCollection = webObject.Lists;
                        SPList myList = myListCollection["Provisioner Scripts"];
                        SPListItemCollection items = myList.GetItems(query);

                        if (items.Count > 0)
                        {
                            TextBox1.Text = items[0]["Script"].ToString();
                        }
                    }
                    catch (SPException)
                    {
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sUrl = SPContext.Current.Web.Url;

            /* do ProcessScript instead of this */
 //           TextReader oTxt = new StringReader(TextBox1.Text);
 //           XPathDocument oDoc = new XPathDocument(oTxt);
 //           XPathNavigator oNav = oDoc.CreateNavigator();

            using (SPSite siteObject = new SPSite(sUrl))
            {
                using (SPWeb web = siteObject.OpenWeb())
                {
                    Interpreter oInterpreter = new Interpreter();
                    oInterpreter.ProcessScript(web, TextBox1.Text);
                }
            }
        }
    }
}
